package com.example.s.fragment.main_child_fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.s.R;
import com.example.s.fragment.BaseFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.DelicacyFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.GameFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.OriginalFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.RecommandFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.TravelFragment;
import com.example.s.fragment.main_child_fragment.discovery_child_fragment.VlogFragment;
import com.example.s.view.MyNav;
import com.example.s.utils.MyUtils;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnMultiListener;

import java.util.ArrayList;
import java.util.List;

import static com.scwang.smart.refresh.layout.constant.RefreshState.ReleaseToRefresh;

public class DiscoveryFragment extends BaseFragment {
    private final String TAG = "DiscoveryFragment";
    private TabLayout navTop;
    private ViewPager vpCotainer;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private RecommandFragment mRecommandFragment;
    private OriginalFragment mOriginalFragment;
    private TravelFragment mTravelFragment;
    private DelicacyFragment mDelicacyFragment;
    private GameFragment mGameFragment;
    private VlogFragment mVlogFragment;
    private SmartRefreshLayout refreshLayout;
    private ClassicsHeader header;
    private ClassicsFooter footer;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_discovery;
    }

    @Override
    public void initView(View view) {

        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        header = (ClassicsHeader) findViewById(R.id.header);
        footer = (ClassicsFooter) findViewById(R.id.footer);
        navTop = (TabLayout) findViewById(R.id.nav_top);
        vpCotainer = (ViewPager) findViewById(R.id.vp_cotainer);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecommandFragment recommandFragment = new RecommandFragment();

        mFragmentList.add(recommandFragment);
        mFragmentList.add(new OriginalFragment());
        mFragmentList.add(new TravelFragment());
        mFragmentList.add(new DelicacyFragment());
        mFragmentList.add(new GameFragment());
        mFragmentList.add(new VlogFragment());


        ArrayList<MyNav.NavItem> navItems = new ArrayList<>();
        navItems.add(new MyNav.NavItem(0, "推荐"));
        navItems.add(new MyNav.NavItem(0, "原创"));
        navItems.add(new MyNav.NavItem(0, "旅行"));
        navItems.add(new MyNav.NavItem(0, "美食"));
        navItems.add(new MyNav.NavItem(0, "游戏"));
        navItems.add(new MyNav.NavItem(0, "vlog"));
        new MyNav(navTop, vpCotainer, navItems, getChildFragmentManager(), mFragmentList, new MyNav.OnMyNavListener() {
            @Override
            public void onCommonListener(ImageView icon, TextView title) {
                title.setTextColor(Color.parseColor("#4DFFFFFF"));
                title.setTextSize(14);
            }

            @Override
            public void onSelectListener(ImageView icon, TextView title, int pos) {
                title.setTextColor(Color.parseColor("#D9408A"));
                title.setTextSize(16);
            }
        });

        refreshLayout.setFooterTriggerRate(1);
        refreshLayout.setOnMultiListener(new OnMultiListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderReleased(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {

            }

            @Override
            public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterReleased(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterFinish(RefreshFooter footer, boolean success) {

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //网络拉取  更多数据    放在底部
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //网络拉取 更多数据 放在头部
                //获取当前是哪个fragment
                int currentItem = vpCotainer.getCurrentItem();
                Log.d(TAG, "onRefresh: " + currentItem);
                refreshLayout.finishRefresh();
            }

            @Override
            public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

                if (refreshLayout.getState() == ReleaseToRefresh)
                    MyUtils.getInstance().vibrate(getActivity(), 20);
            }
        });


        recommandFragment.setOnRefreshListener(new OnRefreshListener<String>() {

            @Override
            public String onRefresh(String tag) {
                return "1";
            }

            @Override
            public String onLoad(String tag) {
                return null;
            }
        });


    }

    private float[] getRgb(int color) {
        float[] aa = new float[4];
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            float alpha = Color.valueOf(color).alpha();
            float red = Color.valueOf(color).red();
            float green = Color.valueOf(color).green();
            float blue = Color.valueOf(color).blue();
            aa[0] = alpha;
            aa[1] = red;
            aa[2] = green;
            aa[3] = blue;
        }
        return aa;

    }


}
